package ui;

import info.Conference;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


/**
 * A panel for viewing individual conference details.  A user can submit a manuscript
 * from this page as long as it is before the conference's submission deadline.
 * @author Aaron Chen
 * @version 3/9/2014
 */
@SuppressWarnings("serial")
public class ConferenceInfoPanel extends AbstractMainComponent {
  
  /** The Conference whose information is to be displayed. */
  private Conference my_conference;
  
  
  /**
   * Create the internal frame, which acts as a panel.
   * @param the_parent The main application window container.
   * @param the_conference The Conference whose information is to be displayed.
   */
  public ConferenceInfoPanel(final DisplayPanel the_parent,
                             final Conference the_conference) {
    super(the_parent);
    my_conference = the_conference;
    setFrameParameters();
    
    setLayout(new BorderLayout(0, 0));
    
    createPanel();
    
    setVisible(true);
  }
  
  /**
   * Creates the panel, which contains information about the conference.
   */
  private void createPanel() {
    final FlowLayout flow_layout = new FlowLayout(FlowLayout.LEFT);
    
    final JPanel panel_title = new JPanel();
    panel_title.setBackground(Standards.COLOR_BACKGROUND);
    add(panel_title, BorderLayout.NORTH);
    
    final JLabel lbl_conference_title = new JLabel(my_conference.getTitle());
    lbl_conference_title.setFont(Standards.FONT_TITLE);
    panel_title.add(lbl_conference_title);
    
    final JPanel panel_information = new JPanel();
    //panel_information.setBackground(Standards.COLOR_BACKGROUND);
    add(panel_information, BorderLayout.CENTER);
    panel_information.setLayout(new BoxLayout(panel_information, BoxLayout.Y_AXIS));
    
    final JPanel panel_categories = new JPanel(flow_layout);
    panel_categories.setBackground(Standards.COLOR_BACKGROUND);
    panel_information.add(panel_categories);
    
    final JLabel lbl_categories = new JLabel();
    final List<String> categories = my_conference.getCategories();
    StringBuilder sb = new StringBuilder("Categories: ");
    if (!categories.isEmpty()) {
      sb.append(categories.get(0));
    }
    for (int i = 1; i < categories.size(); i++) {
      sb.append(", ");
      sb.append(categories.get(i));
    }
    lbl_categories.setText(sb.toString());
    panel_categories.add(lbl_categories);
    
    final JTextArea ta_info = new JTextArea(my_conference.getInfo());
    ta_info.setBackground(Standards.COLOR_BACKGROUND);
    ta_info.setWrapStyleWord(true);
    ta_info.setLineWrap(true);
    ta_info.setEditable(false);
    panel_information.add(ta_info);
    
    final JPanel panel_submission = new JPanel(flow_layout);
    panel_information.add(panel_submission);
    
    final SimpleDateFormat df = Standards.DATE_FORMAT;
    sb = new StringBuilder("Submission Deadline: ");
    sb.append(df.format(my_conference.getSubmissionDeadline()));
    final JLabel lbl_submission_deadline = new JLabel(sb.toString());
    panel_submission.add(lbl_submission_deadline);
    
    final JButton btn_submit_manuscript = new JButton("Submit a Manuscript");
    btn_submit_manuscript.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(final ActionEvent the_e) {
        final DisplayPanel dsp = getDisplayStack();
        final ManuscriptSubmissionPanel msp =
            new ManuscriptSubmissionPanel(dsp, my_conference, MainPage.getUser());
        dsp.addToStack(msp);
      }
    });
    panel_submission.add(btn_submit_manuscript);
  }

  /**
   * Sets up the internal frame's properties.
   */
  protected void setFrameParameters() {
//    setBackground(Standards.COLOR_BACKGROUND);
    setBorder(null);
//    final javax.swing.plaf.InternalFrameUI ifu = getUI();
//    ((javax.swing.plaf.basic.BasicInternalFrameUI) ifu).setNorthPane(null);  
    setBorder(null);
    setFocusable(true);
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public String getPanelLabel() {
    return "Conference";
  }

}
